import numpy as np
import skfuzzy as fuzz
from matplotlib import pyplot as plt

# 生成随机数据
np.random.seed(42)
xpts = np.random.normal(0, 1, 1000)
ypts = np.random.normal(0, 1, 1000)
data = np.vstack((xpts, ypts))

# 设置 FCM 参数
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(
    data, 6, 2, error=0.005, maxiter=1000, init=None)

# 绘制结果
cluster_membership = np.argmax(u, axis=0)
plt.figure(figsize=(8,4), dpi=300)
plt.grid(True)

for j in range(6):
    plt.plot(xpts[cluster_membership == j],
            ypts[cluster_membership == j], 'o')
plt.plot(cntr[:, 0], cntr[:, 1], 'sm', markersize=8)
plt.show()